#coding=utf-8

import json
import config
from application.utilities.ctGet import ZF
from application.utilities.ctAnalytics import ctAnalytics

import urllib
import urllib2

#存储客户端信息
clients = {}

#用于返回统一格式内容
def json_response(error,result,message=""):
    response = {'error':error,'result':result,'message': message}
    return response
    #return json.dumps(response)

def pre(request):
                if not request.has_key('campus_id'):
                    return json_response(True,"","campus_id不能为空！")

                if not config.campus_setting.has_key(request['campus_id']):
                     return json_response(True,"","campus_id不支持！")

                setting = config.campus_setting[request['campus_id']]

                zfClass  = ZF(setting)
                response = zfClass.pre()

                if response['error']:
                    return json_response(True,response['result'],response['message'])

                clients[response['result']['token']] = zfClass

                #返回
                return json_response(False,response['result'],response['message'])

def login(request):
                if not request.has_key('username') or request['username'] == "":
                    return json_response(True,"","学号不能为空！")
                if not request.has_key('password') or request['password'] == "":
                    return json_response(True,"","密码不能为空！")
                if not request.has_key('token') or request['token'] == "":
                    return json_response(True,"","token不能为空！")

                zfClass = clients.get(request['token'], 0)
                if zfClass == 0:
                    return json_response(True,"tokenTimeOut","token过期！")

                response = zfClass.login(request)

                if response['error']:
                    return json_response(True,response['result'],response['message'])

                #返回
                return json_response(False,response['result'],response['message'])


def verify(request):
                if not request.has_key('token') or request['token'] == "":
                    return json_response(True,"","token不能为空！")

                zfClass = clients.get(request['token'], 0)
                if zfClass == 0:
                    return json_response(True,"tokenTimeOut","token过期！")

                #获取
                response = zfClass.get_intro() 

                if response['error']:
                    return json_response(True,response['result'],response['message'])

                #返回
                return json_response(False,response['result'],response['message'])


def intro(request):
                if not request.has_key('token') or request['token'] == "":
                    return json_response(True,"","token不能为空！")

                zfClass = clients.get(request['token'], 0)
                if zfClass == 0:
                    return json_response(True,"tokenTimeOut","token过期！")

                #获取
                response = zfClass.get_intro() 

                if response['error']:
                    return json_response(True,response['result'],response['message'])

                #返回
                return json_response(False,response['result'],response['message'])

def info(request):
                if not request.has_key('token') or request['token'] == "":
                    return json_response(True,"","token不能为空！")

                zfClass = clients.get(request['token'], 0)
                if zfClass == 0:
                    return json_response(True,"tokenTimeOut","token过期！")

                #获取
                response = zfClass.get_info() 

                if response['error']:
                    return json_response(True,response['result'],response['message'])

                #分析
                try:
                    response['result'] = ctAnalytics().info_do(response['result'])
                except KeyError:
                    return json_response(True,"","页面分析出错！")

                #获取
                responsePhoto = zfClass.get_photo()

                if responsePhoto['error']:
                    return json_response(True,responsePhoto['result'],responsePhoto['message'])

                response['result']['zp'] = responsePhoto["result"]

                #返回
                return json_response(False,response['result'],response['message'])

def table(request):
                if not request.has_key('token') or request['token'] == "":
                    return json_response(True,"","token不能为空！")

                zfClass = clients.get(request['token'], 0)
                if zfClass == 0:
                    return json_response(True,"tokenTimeOut","token过期！")

                #获取
                response = zfClass.get_table() 

                if response['error']:
                    return json_response(True,response['result'],response['message'])
                
                #分析
                try:
                    response['result'] = ctAnalytics().table_do(response['result'])
                except KeyError:
                    return json_response(True,"","页面分析出错！")

                #返回
                return json_response(False,response['result'],response['message'])

def exam(request):
                if not request.has_key('token') or request['token'] == "":
                    return json_response(True,"","token不能为空！")

                zfClass = clients.get(request['token'], 0)
                if zfClass == 0:
                    return json_response(True,"tokenTimeOut","token过期！")

                #获取
                response = zfClass.get_exam() 

                if response['error']:
                    return json_response(True,response['result'],response['message'])
                
                #分析
                try:
                    response['result'] = ctAnalytics().exam_do(response['result'])
                except KeyError:
                    return json_response(True,"","页面分析出错！")

                #返回
                return json_response(False,response['result'],response['message'])

def score(request):
                if not request.has_key('token') or request['token'] == "":
                    return json_response(True,"","token不能为空！")

                zfClass = clients.get(request['token'], 0)
                if zfClass == 0:
                    return json_response(True,"tokenTimeOut","token过期！")

                #获取
                response = zfClass.get_score() 

                if response['error']:
                    return json_response(True,response['result'],response['message'])
                
                #分析
                try:
                    response['result'] = ctAnalytics().score_do(response['result'])
                except KeyError:
                    return json_response(True,"","页面分析出错！")

                #返回
                return json_response(False,response['result'],response['message'])

#pre
request = {'campus_id':'1'}
preResult = pre(request)
print preResult

#识别验证码
token = preResult["result"]["token"]
code_row = preResult["result"]["code_row"]

post_data = urllib.urlencode({"system_id":2,"code_row":code_row})
request   = urllib2.Request("http://192.168.2.103/service_code/index.php",post_data)
code = urllib2.build_opener().open(request).read()

print code

#login
request = {'username':'140104010011','password':'807837296qq','code':code,'token':token}
print login(request)

#verify
request = {'token':token}
print info(request)